package com.latsen.pawfit.common.util.glide

import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.support.annotation.DrawableRes
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentActivity
import com.bumptech.glide.load.Transformation
import com.bumptech.glide.request.RequestListener

class GlideImageOptions() {

    var fragment: Fragment? = null
        private set
    var activity: FragmentActivity? = null
        private set

    var placeHolder: GlideResource? = null
        private set

    var error: GlideResource? = null
        private set

    var showAni: Boolean = true
        private set

    var signature: Any? = null
        private set

    var requestListeners: List<RequestListener<Drawable>> = listOf()
        private set

    var refresh: Boolean = false
        private set

    var circleCrop: Boolean = false
        private set

    var transforms: List<Transformation<Bitmap>> = listOf()
        private set

    constructor(fragment: Fragment) : this() {
        this.fragment = fragment
    }

    constructor(activity: FragmentActivity) : this() {
        this.activity = activity
    }

    fun placeHolder(drawable: Drawable) = placeHolder(GlideResource(drawable))
    fun placeHolder(@DrawableRes resId: Int) = placeHolder(GlideResource(resId))
    fun placeHolder(resource: GlideResource) = this.apply {
        this.placeHolder = resource
    }

    fun error(drawable: Drawable) = error(GlideResource(drawable))
    fun error(@DrawableRes resId: Int) = error(GlideResource(resId))
    fun error(resource: GlideResource) = this.apply {
        this.error = resource
    }

    fun showAni(showAni: Boolean) = this.apply {
        this.showAni = showAni
    }

    fun signature(obj: Any) = this.apply {
        this.signature = obj
    }

    fun listener(listeners: List<RequestListener<Drawable>>) = this.apply {
        requestListeners = listeners
    }

    fun listener(listener: RequestListener<Drawable>) = listener(listOf(listener))
    fun listener(vararg listeners: RequestListener<Drawable>) = listener(listeners.toList())


    fun refresh() = this.apply {
        this.refresh = true
    }

    fun transform(transform: Transformation<Bitmap>) = transforms(listOf(transform))
    fun transforms(vararg transform: Transformation<Bitmap>) = transforms(transform.toList())
    fun transforms(transforms: List<Transformation<Bitmap>>) = this.apply {
        this.transforms = transforms
    }

    fun circleCrop() {
        this.circleCrop = true
    }
}